//
//  MessageViewController.m
//  MooreDoll
//
//  Created by Joe on 2014/11/24.
//  Copyright (c) 2014年 Tank. All rights reserved.
//

#import "MessageViewController.h"
#import "MessageRoomCell.h"
#import "RoomViewController.h"
#import "AppDelegate.h"
#include "libxclient.h"
#include "lame.h"
#import "AddMooreDollViewController.h"


@interface MessageViewController ()

@end

@implementation MessageViewController
@synthesize Array_chatRoom,managedObjectContext,chatRoomFromFriendVC,chatRoomTableView,BT_addNewDoll,BT_goToShop,IV_logo;



- (IBAction)openDrawer:(id)sender
{
    [self.menuContainerViewController toggleLeftSideMenuCompletion:nil];
}


-(void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateMessage) name:@"updateMessage" object:nil];
}



-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    if (chatRoomFromFriendVC)
    {
       //如果fromFrendViewsChatRoom 存在，代表是從friendViewController取得聊天室，並直接跳進房間
        
        RoomViewController *rvc = [self.storyboard instantiateViewControllerWithIdentifier:@"RoomViewController"];
        rvc.chatRoom = chatRoomFromFriendVC;
        [self.navigationController pushViewController:rvc animated:YES];
        chatRoomFromFriendVC = nil;
        
        return;
    }
    
    
    //如果chatRoomFromFriendVC不存在，表示是從tabBar的button擊點進來的
    [self updateMessage];
    //}

}
//Tank{

-(void)updateMessage
{
    //取得所有聊天室
    managedObjectContext = [((AppDelegate *)[UIApplication sharedApplication].delegate) managedObjectContext];
    NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"ChatRoom" inManagedObjectContext:managedObjectContext];
    NSArray *sort = @[[NSSortDescriptor sortDescriptorWithKey:@"lastUpdate" ascending:YES]];
    [fetch setEntity:entity];
    [fetch setSortDescriptors:sort];
    
    Array_chatRoom =[managedObjectContext executeFetchRequest:fetch error:nil];
    [self.chatRoomTableView reloadData];
    
    //Tank{
    //如果沒有資料
    if (Array_chatRoom.count >0)
        [self showBuyDollView:NO];
    else
        [self showBuyDollView:YES];
}

-(void)showBuyDollView:(BOOL)isShow{
    
    if (isShow)
    {
        BT_goToShop.hidden = NO;
        BT_addNewDoll.hidden = NO;
        IV_logo.hidden = NO;
        self.chatRoomTableView.hidden = YES;
    }
    else
    {
        BT_goToShop.hidden = YES;
        BT_addNewDoll.hidden = YES;
        IV_logo.hidden = YES;
        self.chatRoomTableView.hidden = NO;
    }
    
}

- (IBAction)AddMooreDoll:(id)sender {
    AddMooreDollViewController *addMooreDollVC = [self.storyboard instantiateViewControllerWithIdentifier:@"AddMooreDollViewController"];
    [self.navigationController pushViewController:addMooreDollVC animated:YES];
}
- (IBAction)testGetRoster:(id)sender {
    [[XMPPSharedInstance sharedInstance]getFriendRoster];
}
//}






#pragma mark UITableViewDelegate and UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return Array_chatRoom.count;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MessageRoomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MessageRoomCell" ];
    
    if (cell == nil)
    {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"MessageRoomCell" owner:self options:nil] objectAtIndex:0];
        ChatRoom *room = [Array_chatRoom objectAtIndex:indexPath.row];
        [cell setCellByChatRoom:room];
    }

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    MessageRoomCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"MessageRoomCell" owner:self options:nil] objectAtIndex:0];
    return cell.frame.size.height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    RoomViewController *rvc = [self.storyboard instantiateViewControllerWithIdentifier:@"RoomViewController"];
    MessageRoomCell *messageCell = (MessageRoomCell *)[tableView cellForRowAtIndexPath:indexPath];
    rvc.chatRoom = messageCell.chatRoom;
    [self.navigationController pushViewController:rvc animated:NO];
    
}

@end
